﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace DVK.SP2007
{
    /// <summary>
    /// The UserInformationFieldMapping deserializes DvkPersonMetadataFieldsMapping setting value to Dictionary<string,string>.
    /// </summary>
    public class UserInfoFieldMapping : IMapping
    {
        string _fieldMapping;
        /// <summary>
        /// Initializes a new instance of the <see cref="UserInfoFieldMapping"/> class.
        /// </summary>
        /// <param name="fieldMapping">The field mapping (e.g. DvkPersonMetadataFieldsMapping value).</param>
        public UserInfoFieldMapping(string fieldMapping)
        {
            _fieldMapping = fieldMapping;
        }

        #region IMapping Members

        /// <summary>
        /// Gets the mapping as key/value pair dictionary.
        /// </summary>
        /// <exception cref="DVK.SP2007.DvkException" />
        /// <returns>mapping</returns>
        public Dictionary<string, string> GetMapping()
        {
            if (string.IsNullOrEmpty(_fieldMapping))
                throw new DvkException("Field mapping string is empty or null.");

            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var mapping = jsonSerializer.Deserialize<Dictionary<string, string>>(_fieldMapping);
            return mapping;
        }

        #endregion
    }
}
